GBML manual / Maps

map_get

Reads the value stored at key in target, a map. If key is not present, nothing is changed and a plain default value is returned instead (0/0.0/false/an empty string/`nothing`, depending on V) - target never crashes. Check map_has(target, key) first if you need to tell "key really maps to this value" apart from "key was not present".

Syntax

map_get(target, key) -> element

Arguments

NameType
targetmap
keymapkey

Returns

element

Description

Reads the value stored at key in target, a map<K,V> - a container that looks values up by key instead of by position.

If key is not present, nothing is changed and a plain default value is returned instead (0/0.0/false/an empty string/nothing, depending on target's value type) - target never crashes. Check map_has(target, key) first if the game needs to tell "key really maps to this value" apart from "key was not present."

map<K,V> has no [i] indexing - unlike array<T>/list<T>/grid<T>, a value is only ever reached by key, through this function. Declare the variable list<T> instead if you need to look inside it by position.

Example

/// @type {map<string,int>}
var scores = {};
map_set(scores, "alice", 10);
var score = map_get(scores, "alice"); // 10
var missing = map_get(scores, "bob"); // 0 - "bob" was never set, so this is the default, not a real score
A real capture - the map holding alice - 10, and GET alice? answering 10A real capture - the map holding alice - 10, and GET alice? answering 10
A real capture - the map holding alice - 10, and GET alice? answering 10
The same map, unchanged - GET bob? answering 0, the default, since bob was never setThe same map, unchanged - GET bob? answering 0, the default, since bob was never set
The same map, unchanged - GET bob? answering 0, the default, since bob was never set

On the GBA

A find() then a ternary (the hash map's find - O(1) average) - deliberately NOT at()/operator[] (both can abort or silently insert on a missing/full key, see this file's own Maps group header). No allocation, no new engine primitive.

See also: map_setmap_hasmap_removemap_lengthmap_clearData containers